package com.fengwk.support.spring.boot.config;

import java.io.IOException;
import java.lang.reflect.Type;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Bean;

import com.fengwk.support.util.ResultUtils;

import feign.FeignException;
import feign.Response;
import feign.codec.DecodeException;
import feign.codec.Decoder;

public abstract class AbstractFeignClientsConfig extends FeignClientsConfiguration {
		
	@Bean
	public Decoder feignJsonMapDecoder() {
		return new JsonMapDecoder(super.feignDecoder());
	}
	
	public static class JsonMapDecoder implements Decoder {
		
		private Decoder decoder;
		
		public JsonMapDecoder(Decoder decoder) {
			this.decoder = decoder;
		}

		@Override
		public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
			if (response != null) {
				String result = (String) decoder.decode(response, String.class);
				return ResultUtils.decodeJsonMapData(result, type);
			} else {
				return decoder.decode(response, type);
			}
		}
		
	}
	
}
